package cn.iocoder.yudao.module.pm.service.thawrecords;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.common.util.number.NumberUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.pm.controller.admin.thawrecords.vo.ThawRecordsPageReqVO;
import cn.iocoder.yudao.module.pm.controller.admin.thawrecords.vo.ThawRecordsSaveReqVO;
import cn.iocoder.yudao.module.pm.controller.app.thawrecords.vo.AppThawRecordsPageReqVO;
import cn.iocoder.yudao.module.pm.controller.app.thawrecords.vo.AppThawRecordsRespVO;
import cn.iocoder.yudao.module.pm.controller.app.thawrecords.vo.AppThawRecordsSaveReqVO;
import cn.iocoder.yudao.module.pm.dal.dataobject.thawrecords.ThawRecordsDO;
import cn.iocoder.yudao.module.pm.dal.mysql.thawrecords.ThawRecordsMapper;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertListByFlatMap;
import static cn.iocoder.yudao.module.pm.enums.ErrorCodeConstants.THAW_RECORDS_NOT_EXISTS;

/**
 * 过程管理-解冻专项台账 Service 实现类
 *
 * @author yisl
 */
@Service
@Validated
public class ThawRecordsServiceImpl implements ThawRecordsService {

    @Resource
    private ThawRecordsMapper thawRecordsMapper;
    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long createThawRecords(ThawRecordsSaveReqVO createReqVO) {
        // 插入
        ThawRecordsDO thawRecords = BeanUtils.toBean(createReqVO, ThawRecordsDO.class);
        thawRecordsMapper.insert(thawRecords);
        // 返回
        return thawRecords.getId();
    }

    @Override
    public void updateThawRecords(ThawRecordsSaveReqVO updateReqVO) {
        // 校验存在
        validateThawRecordsExists(updateReqVO.getId());
        // 更新
        ThawRecordsDO updateObj = BeanUtils.toBean(updateReqVO, ThawRecordsDO.class);
        thawRecordsMapper.updateById(updateObj);
    }

    @Override
    public void deleteThawRecords(Long id) {
        // 校验存在
        validateThawRecordsExists(id);
        // 删除
        thawRecordsMapper.deleteById(id);
    }

    @Override
        public void deleteThawRecordsListByIds(List<Long> ids) {
        // 校验存在
        validateThawRecordsExists(ids);
        // 删除
        thawRecordsMapper.deleteByIds(ids);
        }

    private void validateThawRecordsExists(List<Long> ids) {
        List<ThawRecordsDO> list = thawRecordsMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(THAW_RECORDS_NOT_EXISTS);
        }
    }

    private void validateThawRecordsExists(Long id) {
        if (thawRecordsMapper.selectById(id) == null) {
            throw exception(THAW_RECORDS_NOT_EXISTS);
        }
    }

    @Override
    public ThawRecordsDO getThawRecords(Long id) {
        return thawRecordsMapper.selectById(id);
    }

    @Override
    public PageResult<ThawRecordsDO> getThawRecordsPage(ThawRecordsPageReqVO pageReqVO) {
        return thawRecordsMapper.selectPage(pageReqVO);
    }

    @Override
    public Long appCreateThawRecords(AppThawRecordsSaveReqVO createReqVO) {
        // 插入
        ThawRecordsDO thawRecords = BeanUtils.toBean(createReqVO, ThawRecordsDO.class);
        thawRecords.setThawCode("JD"+System.currentTimeMillis());
        thawRecordsMapper.insert(thawRecords);
        // 返回
        return thawRecords.getId();
    }

    @Override
    public void appUpdateThawRecords(AppThawRecordsSaveReqVO updateReqVO) {
        // 校验存在
        validateThawRecordsExists(updateReqVO.getId());
        // 更新
        ThawRecordsDO updateObj = BeanUtils.toBean(updateReqVO, ThawRecordsDO.class);
        thawRecordsMapper.updateById(updateObj);
    }

    @Override
    public void appDeleteThawRecords(Long id) {
        // 校验存在
        validateThawRecordsExists(id);
        // 删除
        thawRecordsMapper.deleteById(id);
    }

    @Override
    public AppThawRecordsRespVO appGetThawRecords(Long id) {
        ThawRecordsDO thawRecordsDO = thawRecordsMapper.selectById(id);
        AppThawRecordsRespVO respVO = BeanUtils.toBean(thawRecordsDO, AppThawRecordsRespVO.class);
        if (thawRecordsDO != null) {
            AdminUserRespDTO user = adminUserApi.getUser(NumberUtils.parseLong(thawRecordsDO.getCreator()));
            if (user != null) {
                respVO.setCreatorName(user.getNickname());
            }
        }
        return respVO;
    }

    @Override
    public PageResult<AppThawRecordsRespVO> appGetThawRecordsPage(AppThawRecordsPageReqVO pageReqVO) {
        PageResult<ThawRecordsDO> thawRecordsDOPageResult = thawRecordsMapper.appSelectPage(pageReqVO);
        return new PageResult<>(buildThawRecordsDetailList(thawRecordsDOPageResult.getList()), thawRecordsDOPageResult.getTotal());
    }

    private List<AppThawRecordsRespVO> buildThawRecordsDetailList(List<ThawRecordsDO> list) {
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }

        // 1 获取创建人列表
        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(convertListByFlatMap(list,
                contact -> Stream.of(NumberUtils.parseLong(contact.getCreator()), contact.getId())));
        // 2. 转换成 VO
        return BeanUtils.toBean(list, AppThawRecordsRespVO.class, clueVO -> {
            // 1. 设置创建人名称
            MapUtils.findAndThen(userMap, NumberUtils.parseLong(clueVO.getCreator()),
                    user -> clueVO.setCreatorName(user.getNickname()));
        });
    }

}